import '../public/index'
import url from '../data/url'
import request from '../data/request'
import store from 'store'
import {header} from '../data/config'

const base = url.base,
  addDeveloper = request.editDeveloperManage,
  getBankList = request.getBankList,
  search = '/project/stop/detail.json',
  save = '/project/stop/doModify.json'

// 页面标题
const title = '停止时间配置',
  action = '修改';

let form = new Vue({
  el: '#form',
  data: () => ({
    id: '',
    startTime: '',
    endTime: '',
    isDisabled: true
  }),
  created: function () {
    let form = this
    $.ajax({
      type: "POST",
      url: base + search,
      dataType: "json",
      headers: {
        myToken: Cookies.get('myToken')
      },
    }).then(res => {
      form.id = res.object.id
      console.log('response:', res)
      let time, timeArr
      if (res.object.ysNoTime) {
        time = res.object.ysNoTime
        timeArr = time.split('-')
        form.startTime = timeArr[0]
        form.endTime = timeArr[1]
      }
      this.initTime()
    })
  },
  methods: {
    saveTime() {
      let form = this
      let saveData = {
        id: form.id,
        ysNoTime: null
      }
      saveData.ysNoTime = form.startTime + '-' + form.endTime
      console.log(saveData)
      $.ajax({
        type: "POST",
        url: base + save,
        dataType: "json",
        headers: {
          myToken: Cookies.get('myToken')
        },
        data: saveData,
      }).then(res => {
        if (res.code == 0) {
          location.reload()
        }
      })
    },
    initTime() {
      let that = this;

      $(this.$refs.startTime).datetimepicker({
        autoclose: true,
        showMeridian: true,
        minuteStep: 1,
        format: "hh:ii",
        startView: 1,
      })
        .on('changeDate', function (e) {

          that.startTime = $(e.target).val()

        }).data("datetimepicker").picker.addClass('timepicker');

      $(this.$refs.endTime).datetimepicker({
        autoclose: true,
        showMeridian: true,
        minuteStep: 1,
        format: "hh:ii",
        startView: 1,
      })
        .on('changeDate', function (e) {

          that.endTime = $(e.target).val()

        }).data("datetimepicker").picker.addClass('timepicker');
    }
  }
})

function valid() {

  let form_valid,
    $form = $('#form')

  $.validator.setDefaults({ // 只能放在validate规则配置之前

    // 提交事件
    submitHandler: () => {
      console.log('submit!')

      let loading_modal = layer.msg('提交中...', {
        icon: 16,
        time: 0
      })

      form.isLock = true

      $.ajax({
        url: base + addDeveloper,
        type: 'POST',
        contentType: 'application/json',
        headers: header,
        data: JSON.stringify({
          id: form.$data.id,
          enterInfoId: form.$data.id,
          loginName: form.$data.loginName,

        })
      })
        .done(function (res) {
          console.log(res)

          if (res.code === -1) {
            layer.msg(res.message, {
              icon: 5
            })

            return
          }

          layer.msg(`${action}${ title }成功`, {
            icon: 6
          })

          // 重置表单
          $.extend(form.$data, form.$options.data())

          setTimeout(() => {
            window.location = './index.html'
          }, 1000)
        })
        .fail(function (res) {
          console.log(res)

          layer.msg(`添加${ title }失败`, {
            icon: 5
          })

          console.log("error");
        })
        .always(function (res) {

          layer.close(loading_modal)
          form.isLock = false

        })

    }
  })

  // 验证
  form_valid = $form.validate({
    errorPlacement: function (error, element) {
      error.appendTo(element.parents('.col-xs-24'))
    },
    rules: {
      loginName: {
        required: true
      },
      enterprisePrefix: {
        required: true
      }
    }
  })

}